<?php 
	$_SESSION['realpage'] = true;
?>

<?php 
// includes
require_once "./class.GeneralServices.php";

session_start();

function spoofing()
{
	
	if (isset($_POST['argler']) && $_POST['argler'])
	{
		if ($_POST['argler'] == $_SESSION['spoof_preventer'])
		{
			return  true;
		}	
	}
		
	return false;
}

// if user is logged on, else try to log in.
if ((isset($_SESSION['userObj'])) ||
	(( spoofing() ) 
	&& (GeneralServices::getService()->login($_POST['name'], $_POST['password']))))
{
?>

<?php
//$var = GeneralServices::getService()->createGroup('test group 2','test group desc 2','true',$_SESSION['userObj']->uid);

//$RandomGroups = GeneralServices::getService()->searchGroup($_SESSION['userObj']->uid,'test',4);

//get random groups for accordion
$RandomGroups = GeneralServices::getService()->getRandomGroups($_SESSION['userObj']->uid, 4);
$gid1 = null;
$gid2 = null;
$gid3 = null;
$gid4 = null;
switch(count($RandomGroups))
{
	case 1:
		$gid1 = $RandomGroups[0];
	break;
	case 2:
		$gid1 = $RandomGroups[0];
		$gid2 = $RandomGroups[1];
	break;
	case 3:
		$gid1 = $RandomGroups[0];
		$gid2 = $RandomGroups[1];
		$gid3 = $RandomGroups[2];		
	break;
	case 4:
		$gid1 = $RandomGroups[0];
		$gid2 = $RandomGroups[1];
		$gid3 = $RandomGroups[2];
		$gid4 = $RandomGroups[3];
	break;	
}

$RandomFriends = GeneralServices::getService()->getRandomFriends($_SESSION['userObj']->uid, 4);
$fid1 = null;
$fid2 = null;
$fid3 = null;
$fid4 = null;
switch(count($RandomFriends))
{
	case 1:
		$fid1 = $RandomFriends[0];
		break;
	case 2:
		$fid1 = $RandomFriends[0];
		$fid2 = $RandomFriends[1];
		break;
	case 3:
		$fid1 = $RandomFriends[0];
		$fid2 = $RandomFriends[1];
		$fid3 = $RandomFriends[2];
		break;
	case 4:
		$fid1 = $RandomFriends[0];
		$fid2 = $RandomFriends[1];
		$fid3 = $RandomFriends[2];
		$fid4 = $RandomFriends[3];
		break;
}
?>
<!DOCTYPE HTML>
<html>

<head>
<?php require_once "./HtmlIncludes.php";?>
<title>Urban Dynamics</title>
</head>
<body>
<script type="text/javascript">
function LogOut()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    window.location = "./WelcomePage.php";
    }
  };
xmlhttp.open("GET","./WelcomePage.php?kill=true",true);
xmlhttp.send();
}

function setGroupId(gid)
{
	if(gid != null)
	{
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
			    if(xmlhttp.responseText == 'false')
			    {
					alert('false');
			    }
			    else
			    {
			    	ShowGroup(gid);
			    }
			}
		};
		xmlhttp.open("GET","./ajax.php?method=setGid&sid="+"<?php echo session_id(); ?>"+"&gid="+gid,true);
		xmlhttp.send();
	}
}

function setFriendId(fid)
{
	if(fid != null)
	{
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
			    if(xmlhttp.responseText == 'false')
			    {
					alert('false');
			    }
			    else
			    {
			    	ShowGroup(fid);
			    }
			}
		};
		xmlhttp.open("GET","./ajax.php?method=setFid&sid="+"<?php echo session_id(); ?>"+"&fid="+fid,true);
		xmlhttp.send();
	}
}

function SearchGroup(creteria, limit)
{
	if(creteria != null && creteria != '')
	{
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
			    if(xmlhttp.responseText == 'false')
			    {
					alert('false');
			    }
			    else
			    {
				    if(xmlhttp.responseText != "")
				    {
				    	$('#groupSearchResults').css('visibility', 'visible');
				    	$('#groupSearchResults').css('display', 'block');
				    	$('#groupSearchResults').html(xmlhttp.responseText);
				    }else
				    {
				    	$('#groupSearchResults').css('visibility', 'visible');
				    	$('#groupSearchResults').css('display', 'block');
				    	$('#groupSearchResults').html('Oops, we could not find any groups...');
				    }
			    }
			}
		};
		xmlhttp.open("GET","./ajax.php?method=searchGroup&sid="+"<?php echo session_id(); ?>"+"&creteria="+creteria+"&limit="+limit,true);
		xmlhttp.send();
	}
}

function SearchFriend(creteria, limit)
{
	if(creteria != null && creteria != '')
	{
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
		    {
			    if(xmlhttp.responseText == 'false')
			    {
					alert('false');
			    }
			    else
			    {
				    if(xmlhttp.responseText != "")
				    {
				    	$('#friendSearchResults').css('visibility', 'visible');
				    	$('#friendSearchResults').css('display', 'block');
				    	$('#friendSearchResults').html(xmlhttp.responseText);
				    }else
				    {
				    	$('#friendSearchResults').css('visibility', 'visible');
				    	$('#friendSearchResults').css('display', 'block');
				    	$('#friendSearchResults').html('Oops, we could not find any friends...');
				    }
			    }
			}
		};
		xmlhttp.open("GET","./ajax.php?method=searchFriend&sid="+"<?php echo session_id(); ?>"+"&creteria="+creteria+"&limit="+limit,true);
		xmlhttp.send();
	}
}
</script>
	<table style="border: 4px solid grey; width: 99.5%; height: 80px;" class="welcomeMSG MedHeightMargin">
		<tr>
			<td style="width:33%;">			
				<div style="float:left;"><img alt="Urban Dynamics" src="./../Images/UrbanLogo.PNG"/></div>
			</td>
			<td style="width:33%; text-align:center; vertical-align:bottom;">
				<div style="">Hello <?php if(isset($_SESSION['userObj'])){echo $_SESSION['userObj']->Handle;}?></div>
				<input type="hidden" id="UID" value="<?php echo $_SESSION['userObj']->uid;?>"/>
			</td>
			<td style="width:33%; vertical-align: bottom;">	 
				<button id="LogoutButton" name="LogoutButton" onclick="LogOut();" style="float:right; margin-bottom: 10px; margin-right: 10px; border: 2px solid #B4BBCD; font-size: .4em;"> Logout </button>				
			</td> 
		</tr>		
	</table>
	<div class="MainSidebar">
		<div id="accordion">
			<h3><a href="#">Groups</a></h3>
			<div style="padding: 0em; text-align: center;">
			<div>
				<span style="float: left;"><input id="groupsText" type="text" style="margin-left: 15px;" value="Search for a group" onmouseover="RemoveText('groupsText', 'Search for a group')" onmouseout="ReturnText('groupsText', 'Search for a group')" onkeypress="enterPressed(event)"/></span>
			    <div class="ui-state-default ui-corner-all" onclick="$('#groupSearchResults').html(''); $('#groupAccordion').css('display', 'block'); $('#groupsText').val(''); ReturnText('groupsText', 'Search for a group');"><span id="CloseSearchGroupButton">X</span></div>
			    <div id="groupSearchResults" style="visibility: hidden; display: none;"></div>
			    <br>
			</div>
			    <script type="text/javascript">
			    function enterPressed(e)
			    {
			    	if(e.keyCode == 13)
			    	{
			    		SearchGroup($('#groupsText').val(), 4);
			    		$('#groupAccordion').css('display', 'none');
			    	}
			    }
			    </script>
			    <input id="gid1" hidden="true" value="<?php echo $gid1->gid;?>"/>
			    <input id="gid2" hidden="true" value="<?php echo $gid2->gid;?>"/>
			    <input id="gid3" hidden="true" value="<?php echo $gid3->gid;?>"/>
			    <input id="gid4" hidden="true" value="<?php echo $gid4->gid;?>"/>
			    <div id="groupAccordion">
			    	<h5><a href="#" onclick="setGroupId($('#gid1').val());"><?php if(null != $gid1){ echo $gid1->name; }?></a></h5>
			    	<div onclick="setGroupId($('#gid1').val());"><?php if(null != $gid1){ echo $gid1->desc; }?></div>
			    	<h5><a href="#" onclick="setGroupId($('#gid2').val());"><?php if(null != $gid2){ echo $gid2->name; }?></a></h5>
			    	<div onclick="setGroupId($('#gid2').val());"><?php if(null != $gid2){ echo $gid2->desc; }?></div>
			    	<h5><a href="#" onclick="setGroupId($('#gid3').val());"><?php if(null != $gid3){ echo $gid3->name; }?></a></h5>
			    	<div onclick="setGroupId($('#gid3').val());"><?php if(null != $gid3){ echo $gid3->desc; }?></div>
			    	<h5><a href="#" onclick="setGroupId($('#gid4').val());"><?php if(null != $gid4){ echo $gid4->name; }?></a></h5>
			    	<div onclick="setGroupId($('#gid4').val());"><?php if(null != $gid4){ echo $gid4->desc; }?></div>
			    	<br>
			    </div>			    
			</div>
			<h3><a href="#">Friends</a></h3>
			<div style="padding: 0em; text-align: center;">
			<div>
				<span style="float: left;"><input id="friendsText" type="text" style="margin-left: 15px;" value="Search for a friend" onmouseover="RemoveText('friendsText', 'Search for a friend')" onmouseout="ReturnText('friendsText', 'Search for a friend')" onkeypress="fenterPressed(event)"/></span>
			    <div class="ui-state-default ui-corner-all" onclick="$('#friendSearchResults').html(''); $('#friendAccordion').css('display', 'block'); $('#friendsText').val(''); ReturnText('friendsText', 'Search for a friend');"><span id="CloseSearchFriendButton">X</span></div>
			    <div id="friendSearchResults" style="visibility: hidden; display: none;"></div>
			    <br>
			</div>
			    <script type="text/javascript">
			    function fenterPressed(e)
			    {
			    	if(e.keyCode == 13)
			    	{
			    		SearchFriend($('#friendsText').val(), 4);
			    		$('#friendAccordion').css('display', 'none');
			    	}
			    }
			    </script>
			    <input id="fid1" hidden="true" value="<?php echo $fid1->gid;?>"/>
			    <input id="fid2" hidden="true" value="<?php echo $fid2->gid;?>"/>
			    <input id="fid3" hidden="true" value="<?php echo $fid3->gid;?>"/>
			    <input id="fid4" hidden="true" value="<?php echo $fid4->gid;?>"/>			
			    <div id="friendAccordion">
			    	<h5><a href="#" onclick="setFriendId($('#fid1').val());"><?php if(null != $fid1){ echo $fid1->name;}?></a></h5>
			    	<div onclick="setFriendId($('#fid1').val());"><?php if(null != $fid1){ echo $fid1->desc; }?></div>
			    	<h5><a href="#" onclick="setFriendId($('#fid2').val());"><?php if(null != $fid2){ echo $fid2->name; }?></a></h5>
			    	<div onclick="setFriendId($('#fid2').val());"><?php if(null != $fid2){ echo $fid2->desc; }?></div>
			    	<h5><a href="#" onclick="setFriendId($('#fid3').val());"><?php if(null != $fid3){ echo $fid3->name; }?></a></h5>
			    	<div onclick="setFriendId($('#fid3').val());"><?php if(null != $fid3){ echo $fid3->desc; }?></div>
			    	<h5><a href="#" onclick="setFriendId($('#fid4').val());"><?php if(null != $fid4){ echo $fid4->name; }?></a></h5>
			    	<div onclick="setFriendId($('#fid4').val());"><?php if(null != $fid4){ echo $fid4->desc; }?></div>
			    	<br>
			    </div>
			</div>
		</div>
		<div>
			<button id="inboxButton" onclick="ShowInboxPage()" style="width: 100%; text-align: left;">Inbox (#)</button>
			
			<button id="myProfileButton" onclick="ShowProfilePage();" style="width: 100%; text-align: left;">My Profile</button>				
			
			<button id="manageGroupsButton" onclick="ShowManageGroupPage()" style="width: 100%; text-align: left;">Manage Groups</button>	
		</div>
	</div>
	<iframe id="iFrame" style="background-image:url('./../Images/BG<?php echo rand(1, 4);?>.jpg'); background-size: 100%;" src="" onload="resizeFrame($('#iFrame').get()[0])" class="MainFrame"></iframe>
</body>
<script type="text/javascript">
$('#LogoutButton').button();
$('#CloseSearchGroupButton').button();
$('#CloseSearchFriendButton').button();

$(function() {
	$( "#accordion" ).accordion({
		collapsible: true,autoHeight: false,active:false
	});
});	

$(function() {
	$( "#groupAccordion" ).accordion({
		collapsible: true,autoHeight: false,active:false
	});
});

$(function() {
	$( "#friendAccordion" ).accordion({
		collapsible: true,autoHeight: false,active:false
	});
});

$(function() {
	$( "#inboxButton" ).button({ icons: { primary: "ui-icon-bullet" } });
	$( "#myProfileButton" ).button({ icons: { primary: "ui-icon-bullet" } });
	$( "#myGroupsButton" ).button({ icons: { primary: "ui-icon-bullet" } });
	$( "#manageGroupsButton" ).button({ icons: { primary: "ui-icon-bullet" } });	
});
</script>
</html>

<?php
// end if logged users 
} else
{
	$_SESSION['error'] = "Error";
	require './WelcomePage.php'; 
}
?>


<?php 
	$_SESSION['realpage'] = false;
?>